package com.ext.aic.utils;

import android.accessibilityservice.AccessibilityService;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.hardware.HardwareBuffer;
import android.util.Log;
import android.view.Display;

import com.eyinfo.android_pure_utils.events.Action1;
import com.eyinfo.android_pure_utils.snow.Butterfly;
import com.eyinfo.storage.files.FileUtils;

import java.io.File;
import java.util.concurrent.Executors;

public class ScreenRegionUtils {

    private static volatile ScreenRegionUtils instance;

    public static ScreenRegionUtils getInstance() {
        if (instance == null) {
            synchronized (ScreenRegionUtils.class) {
                if (instance == null) {
                    instance = new ScreenRegionUtils();
                }
            }
        }
        return instance;
    }

    public void takeScreenshot(AccessibilityService service, Rect bounds, Action1<Long> doneCallback) {
        try {
            Log.d("conversation", "【3】开始Accessibility截图");
            AccessibilityTakeScreenshot takeScreenshot = new AccessibilityTakeScreenshot(bounds, doneCallback);
            service.takeScreenshot(Display.DEFAULT_DISPLAY, Executors.newCachedThreadPool(), takeScreenshot);
        } catch (Exception e) {
            e.printStackTrace();
            Log.d("conversation", "【6】截图失败:" + e.getMessage());
            if (doneCallback != null) {
                doneCallback.call(null);
            }
        }
    }

    private class AccessibilityTakeScreenshot implements AccessibilityService.TakeScreenshotCallback {

        private Rect bounds;
        private Action1<Long> doneCallback;

        public AccessibilityTakeScreenshot(Rect bounds, Action1<Long> doneCallback) {
            this.bounds = bounds;
            this.doneCallback = doneCallback;
        }

        @Override
        public void onSuccess(AccessibilityService.ScreenshotResult screenshot) {
            long id = Butterfly.getInstance().nextId();
            String fileName = String.format("%s.jpg", id);
            createBitmapAndSave(screenshot, bounds, fileName);
            Log.d("conversation", "【4】截图成功");
            if (doneCallback != null) {
                doneCallback.call(id);
            }
        }

        @Override
        public void onFailure(int errorCode) {
            Log.d("conversation", "【5】截图失败:" + errorCode);
            if (doneCallback != null) {
                doneCallback.call(null);
            }
        }
    }

    private void createBitmapAndSave(AccessibilityService.ScreenshotResult screenshot, Rect bounds, String fileName) {
        try {
            HardwareBuffer hardwareBuffer = screenshot.getHardwareBuffer();
            Bitmap bitmap = Bitmap.wrapHardwareBuffer(hardwareBuffer, null);
            Bitmap regionBitmap = Bitmap.createBitmap(
                    bitmap,
                    bounds.left,
                    bounds.top,
                    Math.abs(bounds.right - bounds.left),
                    Math.abs(bounds.bottom - bounds.top)
            );

            File dir = BoxUtils.getInstance().getAccessibilityScreenshotDir();
            File file = FileUtils.saveBitmap(dir, fileName, regionBitmap);
            hardwareBuffer.close();
            Log.d("conversation", "【7】保存文件成功" + file.getAbsolutePath());
        } catch (Exception e) {
            Log.d("conversation", "【8】保存文件失败:" + e.getMessage());
            e.printStackTrace();
        }
    }
}
